<?php
declare (strict_types = 1);
namespace app\admin\model\market\advert;

use app\common\model\BaseModel;

/**
 * 定义广告图配置数据模型
 */
class Advert extends BaseModel
{
    /**
     * 表名
     * @var string
     */
    protected $name = 'advert';

    /**
     * 设置自动写入时间
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    /**
     * 无需创建时间
     * @var boolean
     */
    protected $createTime = false;
    /**
     * 时间显示格式
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';

    /**
     * 定义表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 全局查询范围
     * @var array
     */
    protected $globalScope = ['mhm'];

    /**
     * 限制机构条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-16
     * @param    [type]                         $query [description]
     * @return   [type]                                [description]
     */
    public function scopeMhm($query)
    {
        // 机构条件
        $query->where('mhm_id', request()->mhm_id);
    }

    /**
     * 广告位置
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-08-21
     * @return   [type]                         [description]
     */
    public function place()
    {
        return $this->belongsTo(AdvertPlace::class, 'place')->bind(['title', 'key', 'app']);
    }

    /**
     * 新增数据事件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-03
     * @return   string
     */
    public static function onBeforeInsert($advert)
    {
        $advert->status = 1;
        $advert->mhm_id = $advert->getAttr('mhm_id') ?: request()->user->getAttr('mhm_id');
    }

    /**
     * 成功创建之后
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-10-14
     */
    public static function onAfterInsert()
    {
        self::cleanCache();
    }

    /**
     * 成功更新之后
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-10-14
     */
    public static function onAfterUpdate()
    {
        self::cleanCache();
    }

    /**
     * 成功删除之后
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-10-14
     */
    public static function onAfterDelete()
    {
        self::cleanCache();
    }

    /**
     * 清理缓存
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-10-14
     * @return [type]                      [description]
     */
    public static function cleanCache() 
    {
        $keys = self::getPlaceKeys();

        array_walk($keys, function ($val, $key) {
            cache("advert_".$val, null);
        });

        if (file_exists('home_pc.txt')) unlink('home_pc.txt');
        if (file_exists('home_ot.txt')) unlink('home_ot.txt');
    }

    /**
     * 获取key值
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-10-14
     * @return [type]                      [description]
     */
    public static function getPlaceKeys()
    {
        return app(AdvertPlace::class)
            ->alias('p')
            ->join('advert a', 'a.place=p.id AND a.mhm_id='.request()->mhm_id)
            ->column('key');
    }
}
